/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.knolle.vistas;

import java.util.Collection;
import org.marcelo.agro.entidades.Actividades;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;

/**
 * Esta ventana muestra todos los datos para guardar una actividad relacionada a una campa;a y un lote en particular
 * puede ser invocada desde la ventana ActividadesTopComponent, o desde el menu, tiene dos constructores, 
 * Debe utilizar el lookup para buscar ver Que Campaña y que lote esta seleccionado,
 * 
 * 
 */
@ConvertAsProperties(dtd = "-//org.knolle.vistas//AddActividades//EN",
autostore = false)
@TopComponent.Description(preferredID = "AddActividadesTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "properties", openAtStartup = false)
@ActionID(category = "Window", id = "org.knolle.vistas.AddActividadesTopComponent")
@ActionReference(path = "Menu/Campañas" /*
 * , position = 333
 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_AddActividadesAction",
preferredID = "AddActividadesTopComponent")
@Messages({
    "CTL_AddActividadesAction=AddActividades",
    "CTL_AddActividadesTopComponent=AddActividades Window",
    "HINT_AddActividadesTopComponent=This is a AddActividades window"
})
public final class AddActividadesTopComponent extends TopComponent implements LookupListener {

    private Lookup.Result result =null;
    private Actividades actividad ;
    
    //
    InstanceContent content = new InstanceContent();
    //
    
    //Variables Finales, para validar los datos ingresados por el usuario
    private static final int MAX_LENTGH_COMENTARIO = 100;
    private static final int MAX_LENTGH_CONTROL = 100;
    private static final int MAX_LENTGH_DOSIS = 100;
    private static final int MAX_LENTGH_SUPERFICIE = 100;
    
    //Este campo es el ID de la ventana a la que debe "Escuchar"
    private static final String VENTANA_LOOKUP = "ActividadesTopComponent";
    
    public AddActividadesTopComponent() {
        initComponents();
        setName(Bundle.CTL_AddActividadesTopComponent());
        setToolTipText(Bundle.HINT_AddActividadesTopComponent());
        putClientProperty(TopComponent.PROP_MAXIMIZATION_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_SLIDING_DISABLED, Boolean.TRUE);
        associateLookup(new AbstractLookup(content));
    }
    
    /**
     * Este metodo esta desarrollado por marcelo para que los datos sean pasados al constructor
     */
    public AddActividadesTopComponent(Actividades actividad){
         
         initComponents();
         cargarDatos(actividad);
         ocultarBotones();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        CampoFecha = new org.jdesktop.swingx.JXDatePicker();
        jLabel2 = new javax.swing.JLabel();
        ComboTipoActividad = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        ComboTipoProducto = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        ComboProducto = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        CampoControl = new javax.swing.JTextField();
        ComboUnidadDosis = new javax.swing.JComboBox();
        CampoDosis = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        CampoSuperficie = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        CampoComentarios = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jLabel2.text")); // NOI18N

        ComboTipoActividad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jLabel3.text")); // NOI18N

        ComboTipoProducto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jLabel4.text")); // NOI18N

        ComboProducto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jLabel5.text")); // NOI18N

        CampoControl.setText(org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.CampoControl.text")); // NOI18N

        ComboUnidadDosis.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        CampoDosis.setText(org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.CampoDosis.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jLabel6.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jLabel7.text")); // NOI18N

        CampoSuperficie.setText(org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.CampoSuperficie.text")); // NOI18N

        CampoComentarios.setColumns(20);
        CampoComentarios.setRows(5);
        CampoComentarios.setText(org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.CampoComentarios.text")); // NOI18N
        jScrollPane1.setViewportView(CampoComentarios);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel8, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jLabel8.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButton3, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jButton3.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButton2, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jButton2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(AddActividadesTopComponent.class, "AddActividadesTopComponent.jButton1.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(0, 94, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6))
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(CampoDosis)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ComboUnidadDosis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1)
                            .addComponent(ComboTipoActividad, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ComboTipoProducto, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ComboProducto, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(CampoControl)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(CampoSuperficie)
                                .addGap(62, 62, 62))
                            .addComponent(CampoFecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(26, 26, 26))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(CampoFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ComboTipoActividad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ComboTipoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ComboProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CampoControl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ComboUnidadDosis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CampoDosis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CampoSuperficie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea CampoComentarios;
    private javax.swing.JTextField CampoControl;
    private javax.swing.JTextField CampoDosis;
    private org.jdesktop.swingx.JXDatePicker CampoFecha;
    private javax.swing.JTextField CampoSuperficie;
    private javax.swing.JComboBox ComboProducto;
    private javax.swing.JComboBox ComboTipoActividad;
    private javax.swing.JComboBox ComboTipoProducto;
    private javax.swing.JComboBox ComboUnidadDosis;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening

//        Aqui realiazr las consultas para cargar los datos en la ventana
//        result = WindowManager.getDefault().findTopComponent(VENTANA_LOOKUP).getLookup().lookupResult(Actividades.class);
//        result.addLookupListener(this);
//        resultChanged(new LookupEvent(result));

    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
//        result.removeLookupListener(this);
//        result = null;
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
    
    /**
     * Metodo cargar los datos en cada uno de los Items del formulario
     * @param actividad 
     */
    void cargarDatos(Actividades actividad){
        
        this.CampoControl.setText(actividad.getControl());
        this.CampoComentarios.setText(actividad.getComentarios());
        this.CampoFecha.setDate(actividad.getFecha());
        this.CampoSuperficie.setText(actividad.getSuperficie().toString());
        this.CampoDosis.setText(actividad.getDosis().toString());
        //cargar los combos
        this.ComboTipoActividad.setSelectedIndex(actividad.getTipoActividad().getId());
        this.ComboTipoProducto.setSelectedIndex(actividad.getTipoActividad().getId());
        this.ComboProducto.setSelectedIndex(actividad.getTipoActividad().getId());
        this.ComboUnidadDosis.setSelectedIndex(actividad.getTipoActividad().getId());        
          
        
        
        
    }
    
    void ocultarBotones(){
        this.jButton1.setVisible(false);
        this.jButton2.setVisible(false);
        this.jButton3.setVisible(false);
    }
    
    //
    @Override
    public void resultChanged(LookupEvent le) {
        //throw new UnsupportedOperationException("Not supported yet.");
        //Obtener el elemento Actividades, cargar cada uno de los campos en los campos de la vista
        //
        Lookup.Result r = (Lookup.Result) le.getSource();
        Collection<Actividades> actividades = (Collection<Actividades>) r.allInstances();
        if (actividades != null) {
            //Teoricamente solo debe haber una instancia en el lookup
            //obtengo la instancia y cargo los datos en los campos
            for(Actividades a : actividades){
                cargarDatos(a);
            }
        }
    }
}
